/**
 * 
 */
package com.ywm8.ywm.admin.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ywm8.ywm.admin.service.NoticeService;
import com.ywm8.ywm.domain.Admin;
import com.ywm8.ywm.domain.Notice;
import com.ywm8.ywm.utils.MD5;
import com.ywm8.ywm.utils.ValidateUtil;

/**
 * @author wangruishun
 * @version ywm8 1.0 2014-3-11 下午7:26:56
 */
@Controller("noticeAction")
@Scope("prototype")
public class NoticeAction extends ActionSupport implements ServletContextAware {
	private static final long serialVersionUID = 8463095287314159925L;
	private File indexPicture;
	private File pagePicture;
	private String indexPictureFileName;
	private String pagePictureFileName;
	private Notice notice;
	// 错误提示
	private String msg;
	@Resource
	private NoticeService ns;
	Admin admin = (Admin) ServletActionContext.getRequest().getSession()
			.getAttribute("admin");
	private ServletContext sc;
	private List<Notice> notices;

	/**
	 * 是否登陆
	 */
	public boolean isLogin() {
		if (admin == null) {
			this.msg = "您未登陆";
			return false;
		}
		return true;
	}

	/**
	 * 添加广告
	 */
	public String managePage() {
		return SUCCESS;
	}

	public String addNotice() {
		if (isLogin()) {
			if (ValidateUtil.isValid(indexPictureFileName)
					&& ValidateUtil.isValid(pagePictureFileName)) {
				String dir = sc.getRealPath("/upload");
				long l = System.nanoTime();
				String ext = indexPictureFileName
						.substring(indexPictureFileName.lastIndexOf("."));
				File newFile = new File(dir, l + ext);
				indexPicture.renameTo(newFile);
				notice.setIndexPicture("/upload/" + l + ext);
				l = System.nanoTime();
				ext = indexPictureFileName.substring(pagePictureFileName
						.lastIndexOf("."));
				File newFile2 = new File(dir, l + ext);
				pagePicture.renameTo(newFile2);
				notice.setPagePicture("/upload/" + l + ext);
				notice.setAid(admin.getId());
				ns.saveEntity(notice);
				return SUCCESS;
			} else
				this.msg = "没有选择图片";
			return "wrong";
		}
		return "notLogin";
	}

	/**
	 * 编辑广告
	 */
	public String editAd() {
		if (isLogin()) {
			String hql = "from Notice n where n.aid=?";
			notices = ns.findEntityByHQL(hql, admin.getId());
			return SUCCESS;
		}
		return "notLogin";
	}

	/**
	 * @return the notices
	 */
	public List<Notice> getNotices() {
		return notices;
	}

	/**
	 * @param notices
	 *            the notices to set
	 */
	public void setNotices(List<Notice> notices) {
		this.notices = notices;
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the indexPicture
	 */
	public File getIndexPicture() {
		return indexPicture;
	}

	/**
	 * @param indexPicture
	 *            the indexPicture to set
	 */
	public void setIndexPicture(File indexPicture) {
		this.indexPicture = indexPicture;
	}

	/**
	 * @return the pagePicture
	 */
	public File getPagePicture() {
		return pagePicture;
	}

	/**
	 * @param pagePicture
	 *            the pagePicture to set
	 */
	public void setPagePicture(File pagePicture) {
		this.pagePicture = pagePicture;
	}

	/**
	 * @return the indexPictureFileName
	 */
	public String getIndexPictureFileName() {
		return indexPictureFileName;
	}

	/**
	 * @param indexPictureFileName
	 *            the indexPictureFileName to set
	 */
	public void setIndexPictureFileName(String indexPictureFileName) {
		this.indexPictureFileName = indexPictureFileName;
	}

	/**
	 * @return the pagePictureFileName
	 */
	public String getPagePictureFileName() {
		return pagePictureFileName;
	}

	/**
	 * @param pagePictureFileName
	 *            the pagePictureFileName to set
	 */
	public void setPagePictureFileName(String pagePictureFileName) {
		this.pagePictureFileName = pagePictureFileName;
	}

	/**
	 * @return the notice
	 */
	public Notice getNotice() {
		return notice;
	}

	/**
	 * @param notice
	 *            the notice to set
	 */
	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	/**
	 * @return the ns
	 */
	public NoticeService getNs() {
		return ns;
	}

	/**
	 * @param ns
	 *            the ns to set
	 */
	public void setNs(NoticeService ns) {
		this.ns = ns;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet
	 * .ServletContext)
	 */
	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;

	}

}
